<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>DOM购物车练习</title>
  <style>
    * {
      user-select: none;
    }

    table {
      width: 600px;
      border-collapse: collapse;
      border: 1px solid black;
    }

    table thead {
      background-color: lightgray;
      border: 1px solid black;
      line-height: 30px;
    }

    table th,
    table td {
      width: 20%;
      text-align: center;
      border: 1px solid black;
    }

    tbody tr td:nth-child(3)>span {
      display: inline-block;
      margin: 0 6px;
      width: 15px;
    }
  </style>
</head>

<body>
  <div id="box">
    <table>
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th>商品</th>
          <th>单价</th>
          <th>数量</th>
          <th>库存</th>
          <th>总计</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>iPhone 15 Pro Max</td>
          <td>¥8999</td>
          <td>
            <button>-</button>
            <span>0</span>
            <button>+</button>
          </td>
          <td></td>
          <td>¥8999</td>
        </tr>
      </tbody>
    </table>
  </div>

  <script>
    var data = [{
        name: "iPhone 11",
        price: 5888,
        count: 2
      },
      {
        name: "iPhone 11 Pro",
        price: 8888,
        count: 3
      },
      {
        name: "iMac Pro",
        price: 18888,
        count: 3
      },
      {
        name: "imac",
        price: 14888,
        count: 2
      },
      {
        name: "iwatch",
        price: 2399,
        count: 4
      },
    ];
    //第一步,遍历数据,将其添加至table
    const els = data.map((value) => {
      console.log(value);
      const {
        name,
        price,
        count
      } = value;
      return `
        <tr>
            <td>${name}</td>
            <td>¥${price}</td>
            <td>
              <button ${count == 1 ? "disabled" : ""}>-</button>
              <span>0</span>
              <button>+</button>
            </td>
            <td>${count}</td>
            <td></td>
          </tr>
        `;
    });
    const tb = document.querySelector("tbody");
    tb.innerHTML = els.join("");

    //第二步 遍历数量 添加按钮点击事件
    const tds = document.querySelectorAll("tbody>tr>td:nth-child(3)");
    // console.log(tds);
    //遍历出来的每一列里的都有按钮点击事件,所以得写在里面

    tds.forEach((td) => {
      const [btn1, num, btn2] = td.children;
      btn1.onclick = function () {
        if (num.innerHTML == 1) {
          btn1.disabled = true;
        }
        num.innerHTML--;
        total();
        btn2.disabled = false;
      };
      btn2.onclick = function () {
        const con = document.querySelector("tbody>tr>td:nth-child(3)>span")
        console.log(con)
        if (num.innerHTML == con.count) {
          btn2.disabled = true;
        }
        btn1.disabled = false;
        num.innerHTML++;
        total();
      };

      //第三步 ,给所有的按钮添加方法,点击+时 小计增加,点击-时,小计减少
      function total() {
        const n = num.innerHTML;
        const td_price = td.previousElementSibling;
        const p = td_price.innerHTML.replace("¥", "");
        const ku = td.nextElementSibling;
        const total_price = ku.nextElementSibling
        total_price.innerHTML = "¥" + n * p;
      }
    })
  </script>
</body>

</html>